package com.lepu.app.fun.hospital.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.DateUtilBase;
import com.eyzhs.app.R;
import com.lepu.app.fun.chat.bean.ChatSession;
import com.lepu.app.fun.chat.bean.UserInfo;
import com.lepu.app.fun.chat.business.chatcore.ChatManager;
import com.lepu.app.fun.chat.business.uiflow.UIFlowManager;
import com.lepu.app.fun.hospital.bean.HospitalDoctor;
import com.lepu.app.fun.knowledge.bean.Article;

import java.util.ArrayList;

import common.db.Constants;

public class LectureListAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater = null;
    private Context mContext = null;
    private ArrayList<Article> mArrayList = null;

    public LectureListAdapter(Context context, ArrayList<Article> arrayList) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mArrayList = arrayList;
    }

    @Override
    public int getCount() {
        return mArrayList.size();
//        return 30;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.mother_item_hospital_lecture, null);
            holder = new ViewHolder();
            holder.txt_title = (TextView) convertView.findViewById(R.id.txt_title);
            holder.txt_content = (TextView) convertView.findViewById(R.id.txt_content);
            holder.txt_time = (TextView) convertView.findViewById(R.id.txt_time);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Article item = mArrayList.get(position);
        holder.txt_title.setText(item.ArticleTitle);
        holder.txt_content.setText(item.ArticleDescription);
        holder.txt_time.setText(DateUtilBase.friendly_time(item.PublishDate));

//        holder.layout_more.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //todo
//            }
//        });
//
//        holder.txtviewChat.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                sendMesage(item);
//            }
//        });

        return convertView;
    }

    private static class ViewHolder {
        TextView txt_title;
        TextView txt_content;
        TextView txt_time;
    }


    private void sendMesage(HospitalDoctor doctor) {
        boolean flag = LoginUtil.jumpLogin((BaseFragmentActivity) mContext);
        if (flag) {
            return;
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(Long.valueOf(doctor.MobilePhone));
        userInfo.setProfilePictureThumbnailId(doctor.Avatar);
        userInfo.setNickName(doctor.DoctorName);

        Bundle bundle = new Bundle();
        ChatSession chatSession =
                ChatManager.getInstance().getOrCreateChatSession(Long.valueOf(LoginUtil.getUserId()), userInfo);
        bundle.putSerializable(Constants.Chat.CHAT_SESSION, chatSession);
        bundle.putSerializable(Constants.USER_INFO, userInfo);
        UIFlowManager.getInstance().performGoActivityAction((BaseFragmentActivity) mContext, "gotoChattingFragment", bundle);
    }
}
